package com.example.it.testformartialdesign;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private NavigationView nav_view;
    private CoordinatorLayout coordinatorLayout;
    private FloatingActionButton floatingBtn;
    private RecyclerView recyclerView;

    int[] frutdrawIds;
    private List<Fruit> fruitList;
    private AppBarLayout appBarLayout;
    private SwipeRefreshLayout swipeRefreshLayout;

    //region Init

    //初始化图片数据
    private void initDrawRes() {

        frutdrawIds = new int[]{
                R.drawable.orangered,
                R.drawable.orange_yellow,
                R.drawable.watermelon,
                R.drawable.strawberry,
                R.drawable.grape,
                R.drawable.grapegreen
        };

    }

    //初始化水果的数据
    private void initData() {

        Random random = new Random();
        fruitList = new ArrayList<Fruit>();
        for (int i = 0; i < 25; i++) {
            int index = (random.nextInt(this.frutdrawIds.length));

            System.out.println(index);

            Fruit fruit = new Fruit("Furt " + i, index);

            fruitList.add(fruit);
        }
        random = null;

        FruitAdapter adapter = new FruitAdapter(this, this.fruitList, this.frutdrawIds);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);

        this.recyclerView.setAdapter(adapter);
    }

    //初始化ActionBar
    private void initActionBar() {
        //将我们的ActionBar作为我们的ActionBar
        this.setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.dl_Home);
        //获取我们的actionbar
        ActionBar bar = this.getSupportActionBar();
        if (bar != null) {
            //将我门的导航按钮显示出来
            bar.setDisplayHomeAsUpEnabled(true);
            //更改默认的图片
            bar.setHomeAsUpIndicator(R.drawable.navigate);
        }
    }

    //初始化findviewbyid
    private void initFindView() {
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.cl_Home);
        nav_view = (NavigationView) findViewById(R.id.nav_View);
        toolbar = (Toolbar) findViewById(R.id.toolbar_HomeActivity);
        floatingBtn = (FloatingActionButton) findViewById(R.id.btn_Del);
        this.recyclerView = (RecyclerView) findViewById(R.id.rv_fruit);

        appBarLayout = (AppBarLayout) this.findViewById(R.id.appbarlayout);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
    }

    //初始化菜单
    private void initMenu() {
        //设置菜单的选中项目
        nav_view.setCheckedItem(R.id.nav_task);

        //为navigation添加菜单
        this.nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //将侧滑菜单关闭
                drawerLayout.closeDrawers();

                return true;
            }
        });


        floatingBtn.setOnClickListener(this);
    }

    //初始化事件
    private void initEvent() {
        floatingBtn.setOnClickListener(this);
        this.swipeRefreshLayout.setOnRefreshListener(this);
    }

    private void initProperty() {
        this.swipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary);
    }
    //endregion


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initDrawRes();

        initFindView();
        initActionBar();
        initMenu();
        initEvent();
        //初始化data
        initData();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //两种方式都可填充菜单
        this.getMenuInflater().inflate(R.menu.menu_home_option, menu);
        //this.toolbar.inflateMenu(R.menu.menu_home_option);

        //这种方式虽然可以填充菜单，但是不能将我们想要显示在Toolbar上的菜单显示出来
        //MenuInflater menuInflater = new MenuInflater(this);
        //menuInflater.inflate(R.menu.menu_home_option, menu);

        return super.onCreateOptionsMenu(menu);
    }

    //region Event
    //在这里捕获导航按钮的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //HomneAdUp按钮的id事 android.R.id.Home
            case android.R.id.home:
                this.drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    //悬浮的删除按钮的点击事件
    @Override
    public void onClick(View view) {
        /*
        *view : 这里一般填写触发事件的本身
        * */
        Snackbar.make(this.floatingBtn, "确认删除该记录!", Snackbar.LENGTH_LONG)
                .setAction("Yes", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(HomeActivity.this, "Deleted!", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    @Override
    public void onRefresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initData();

                        //取消下拉刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                });

            }
        }).start();
    }
    //endregion
}
